package cn.ds.assets.module.assets.dal.mysql.dataQualityRuleSet;

import java.util.*;
import java.time.LocalDateTime;
import java.time.LocalDate;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.ds.assets.framework.mybatis.core.mapper.BaseMapperX;
import cn.ds.assets.module.assets.dal.dataobject.dataQualityRuleSet.BaseDataQualityRuleSetDO;
import org.apache.ibatis.annotations.Mapper;
import cn.ds.assets.module.assets.controller.admin.dataQualityRuleSet.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 数据质量规则设置 Mapper
 *
 * @author admin
 */
@Mapper
public interface BaseDataQualityRuleSetMapper extends BaseMapperX<BaseDataQualityRuleSetDO> {

    default PageResult<BaseDataQualityRuleSetDO> selectPage(BaseDataQualityRuleSetPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BaseDataQualityRuleSetDO>()
                .eqIfPresent(BaseDataQualityRuleSetDO::getContent, reqVO.getContent())
                .eqIfPresent(BaseDataQualityRuleSetDO::getRegularExpression, reqVO.getRegularExpression())
                .eqIfPresent(BaseDataQualityRuleSetDO::getFormatType, reqVO.getFormatType())
                .eqIfPresent(BaseDataQualityRuleSetDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseDataQualityRuleSetDO::getQualityRuleId, reqVO.getQualityRuleId())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseDataQualityRuleSetDO::getRuleCondition, reqVO.getRuleCondition())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCron, reqVO.getCron())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCustomSql, reqVO.getCustomSql())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareDb, reqVO.getCompareDb())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareTable, reqVO.getCompareTable())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareColumn, reqVO.getCompareColumn())
                .eqIfPresent(BaseDataQualityRuleSetDO::getUnit, reqVO.getUnit())
                .inIfPresent(BaseDataQualityRuleSetDO::getId,reqVO.getIds())
                .orderByDesc(BaseDataQualityRuleSetDO::getId));
    }

    default List<BaseDataQualityRuleSetDO> selectList(BaseDataQualityRuleSetExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BaseDataQualityRuleSetDO>()
                .eqIfPresent(BaseDataQualityRuleSetDO::getContent, reqVO.getContent())
                .eqIfPresent(BaseDataQualityRuleSetDO::getRegularExpression, reqVO.getRegularExpression())
                .eqIfPresent(BaseDataQualityRuleSetDO::getFormatType, reqVO.getFormatType())
                .eqIfPresent(BaseDataQualityRuleSetDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(BaseDataQualityRuleSetDO::getQualityRuleId, reqVO.getQualityRuleId())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseDataQualityRuleSetDO::getRuleCondition, reqVO.getRuleCondition())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCron, reqVO.getCron())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(BaseDataQualityRuleSetDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCustomSql, reqVO.getCustomSql())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareDb, reqVO.getCompareDb())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareTable, reqVO.getCompareTable())
                .eqIfPresent(BaseDataQualityRuleSetDO::getCompareColumn, reqVO.getCompareColumn())
                .eqIfPresent(BaseDataQualityRuleSetDO::getUnit, reqVO.getUnit())
                .inIfPresent(BaseDataQualityRuleSetDO::getId,reqVO.getIds())
                .orderByDesc(BaseDataQualityRuleSetDO::getId));
    }

    @Update("UPDATE assets_data_quality_rule_set SET deleted = 1, update_time = #{deletedTime} WHERE id = #{id}")
    void updateToDelete(@Param("id") Long id, @Param("deletedTime") LocalDateTime deletedTime);
}
